#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vi: ts=4 expandtab
#
# Copyright (C) 2012 KT
#
# Author: Choonho Son <choonho.son@kt.com>
#

import subprocess
from cloudcfg.lib.cmd import Cmd

__version__="1.0"

class cmd_ssh(Cmd):
	def __init__(self, logger):
		Cmd.__init__(self,logger)

	def ssh_keygen(self, params):
		"""
		create private, public key pair
		return key files path
		
		@params (dictionary)
		ssh-keygen : /usr/bin/ssh-keygen (binary path)
		encrypt : rsa | dsa    (default=rsa)
		phrase  : Passphrase   (default=none)
		path    : key saving folder (default='/etc/ssh')
		
		@return (private, public key path) : absolute key files path
		"""
		p = params
		cmd = [ p['ssh-keygen'], '-q', '-t', p['encrypt'], '-f', p['path'], '-P',p['phrase']]
		self.run(cmd)

	def add2known_hosts(self, pubKey, dst):
		"""
		@param pubKey : public Key file path
		@param dst : destination path (known_hosts)
		"""
		fp = open(pubKey,'r')
		dstfp = open(dst, 'a+')
		dstfp.write(fp.read())
		fp.close()
		dstfp.close()

	def add2authorized_keys(self, pubKey, dst):
		fp = open(pubKey,'r')
		dstfp = open(dst, 'a+')
		dstfp.write(fp.read())
		fp.close()
		dstfp.close()
										
